package cn.myjerry.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 车辆信息管理系统
 * @author Jerry
 *
 */

public class MapDemo04 {
	public static void main(String[] args) {
		List<Car> list = exam();
		Map<String, CarBrand> map = count(list);
		view(map);
	}
	
	public static void view(Map<String, CarBrand> map){
		Set<String> keySet = map.keySet();
		Iterator<String> keyIt = keySet.iterator();
		while (keyIt.hasNext()) {
			String type = keyIt.next();
			CarBrand brand = map.get(type);
			int total = brand.getTotal();
			int avg = total/brand.getList().size();
			System.out.println(type+"==>"+total+"==>"+avg);
		}
	}
	
	/**
	 * 统计分析
	 * 1、面向对象
	 * 2、分拣存储
	 * @param list
	 * @return
	 */
	public static Map<String, CarBrand> count(List<Car> list){
		Map<String, CarBrand> map = new HashMap<String, CarBrand>();
		for(Car car:list){
			String type = car.getType();
			int num = car.getMoney();
			CarBrand brand = map.get(type);
			if(null == brand){
				brand = new CarBrand();
				map.put(type, brand);
			}
			brand.getList().add(car);
			brand.setTotal(brand.getTotal() + num);
		}
		return map;
	}
	
	/**
	 * 模拟车辆
	 * @return
	 */
	public static List<Car> exam(){
		List<Car> list = new ArrayList<Car>();
		list.add(new Car("君威", "别克", 130000, 10));
		list.add(new Car("威朗", "别克", 140000, 10));
		list.add(new Car("昂科威", "别克", 150000, 10));
		list.add(new Car("秦", "比亚迪", 160000, 10));
		list.add(new Car("明", "比亚迪", 170000, 10));
		list.add(new Car("元", "比亚迪", 180000, 10));		
		
		return list;
	}
	
}
